<template>
    <div class="control list_container flex both-100">
        <Breadcrumb :title="pageTitle" />
        <div class="content_container">
            <el-form ref="searchForm" :inline="true" :model="searchMap" class="search_wrapper flex-space-between">
                <div class="search">
                    <el-form-item prop="dbIncrementSize" size="small">
                        <el-input v-model="searchMap.dbIncrementSize" placeholder="增量大小"></el-input>
                    </el-form-item>
                    <el-form-item class="flex">
                        <el-button @click="handleSearch()" class="btn_primary el-icon-search">查询</el-button>
                        <el-button @click="handleReset()" class="btn_cancel el-icon-refresh-left">重置</el-button>
                    </el-form-item>
                </div>
                <div class="edit">
                    <el-form-item class="flex">
                        <el-button @click="handleAdd()" class="btn_primary el-icon-plus">新增</el-button>
                    </el-form-item>
                </div>
            </el-form>
            <div class="table_container">
                <!-- 表格 -->
                <el-table :data="dataList" v-loading="loading" stripe class="per_table" style="width: 100%"
                    :height="winTableH">
                    <el-table-column prop="dbIncrementSize" label="增量大小"></el-table-column>
                    <el-table-column prop="dbSize" label="大小" align="center"></el-table-column>
                    <el-table-column prop="createTime" label="创建时间" align="center"></el-table-column>
                    <el-table-column prop="remark" label="备注"></el-table-column>
                    <el-table-column label="操作" width="280">
                        <template slot-scope="scope">
                            <div>
                                <span type="text" class="tag_primary" size="small"
                                    @click="handleSelect(scope.row)">查看</span>
                                <span type="text" class="marginl-10 tag_primary" size="small"
                                    @click="handleEdit(scope.row)">编辑</span>
                                <span type="text" class="marginl-10 tag_warning" size="small"
                                    @click="handleDelete(scope.row)">删除</span>
                            </div>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <!-- 分页 -->
            <Pagination v-show="total > 0" :total="total" :page.sync="searchMap.pageNum" :limit.sync="searchMap.pageSize"
                @change="getList"></Pagination>
        </div>

        <!-- 弹窗 详情 -->
        <el-dialog class="m_dialog" :title="title" :visible.sync="dialogVisible" width="800" append-to-body
            :before-close="() => { dialogVisible = false }" :close-on-click-modal="false">
            <div class="content">
                <el-form ref="form" :model="formModel" size="small" :rules="formRules" label-width="80px">
                    <el-row :gutter="20">
                        <el-col :span="24">
                            <el-form-item label="增量大小" label-width="120px" prop="dbIncrementSize">
                                <el-input v-model="formModel.dbIncrementSize" placeholder="请输入增量大小"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label="大小" label-width="120px" prop="dbSize">
                                <el-input v-model="formModel.dbSize" type="textarea"
                                    placeholder="请输入大小"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label="备注" label-width="120px" prop="remark">
                                <el-input v-model="formModel.remark" type="textarea"
                                    placeholder="请输入大小"></el-input>
                            </el-form-item>
                        </el-col>

                    </el-row>
                </el-form>
            </div>
            <div slot="footer" class="dialog-footer">
                <el-button @click="() => { dialogVisible = false }" class="btn_cancel">取 消</el-button>
                <el-button type="primary" @click="submitFrom()" class="btn_primary">确 定</el-button>
            </div>
        </el-dialog>
        <el-dialog :title="title" :visible.sync="dialogVisibleInfo" width="800" append-to-body>
            <custom-title title="详细信息" />
            <BaseDescriptions :descConfig="descConfig" :descConfigItem="descConfigItem" :descModel="formModel">
            </BaseDescriptions>
        </el-dialog>
    </div>
</template>
<script>
import { dbInfoHandle, dbInfoList } from '../api/index'

import BaseDescriptions from '@/components/PageContent/BaseDescriptions.vue';

export default {
    dicts: ['control_hierarchy'],
    components: {
        BaseDescriptions,
    },
    data() {
        return {
            pageTitle: '数据存储',
            loading: true,
            total: 0,
            title: '数据存储详情',
            dialogVisible: false,
            dialogVisibleInfo: false,
            deptOptions: undefined,
            userOptions: [],
            searchMap: {
                pageNum: 1,
                pageSize: 10,
            },
            dataList: [],
            // 提交表单数据
            formModel: {},
            reqType: 'post',
            formRules: {
                dbIncrementSize: {
                    required: true,
                    message: "增量大小不能为空",
                    trigger: "change",
                },
                dbSize: {
                    required: true,
                    message: "大小不能为空",
                    trigger: "change",
                },
            },
            descConfig: {
                size: 'medium',
                column: '2',
            },
            descConfigItem: [
                {
                    fieIdName: 'dbIncrementSize',
                    label: '增量大小',
                    span: 2
                },
                {
                    fieIdName: 'dbSize',
                    label: '大小',
                    span: 2,
                },
                {
                    fieIdName: 'createTime',
                    label: '创建时间',
                    span: 2,
                },
                {
                    fieIdName: 'remark',
                    label: '备注',
                    span: 2,
                },
            ],
        }
    },
    computed: {
    },
    methods: {
        submitFrom() {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    dbInfoHandle(this.formModel, this.reqType).then(res => {
                        if (200 === res.code) {
                            this.$message({
                                message: '操作成功',
                                type: 'success',
                            });
                            this.dialogVisible = false
                            this.formModel = {}
                            this.getList()
                        }
                    });
                } else {
                    return false;
                }
            });
        },
        //删除
        handleDelete(row) {
            this.$confirm('此操作将永久删除该条数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                dbInfoHandle([row.dbInfoId], 'delete').then(res => {
                    if (200 === res.code) {
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                        this.getList()
                    }
                });
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        // 查询
        handleSearch() {
            this.getList()
        },
        // 重置
        handleReset() {
            this.searchMap = {
                pageNum: 1,
                pageSize: 10,
            }
            this.getList()
        },
        //新增打开
        handleAdd() {
            this.title = "新增数据存储"
            this.reqType = 'post'
            if (this.$refs.form) {
                this.$refs.form.resetFields()
            }
            this.formModel = {}
            this.dialogVisible = true;
        },
        //编辑
        handleEdit(row) {
            this.title = "编辑数据存储"
            this.formModel = row
            this.reqType = 'put'
            this.dialogVisible = true;
        },
        //查看
        handleSelect(row) {
            this.title = "数据存储详情"
            this.reqType = 'select'
            this.formModel = row
            this.dialogVisibleInfo = true;
        },
        // 页面数据
        async getList() {
            const { code, rows, total } = await dbInfoList(this.searchMap)
            if (code === 200) {
                this.loading = false
                this.dataList = rows
                this.total = total
            }
        },
    },
    mounted() {
        this.getList()
    },
}
</script>
<style lang="less">
.control {
    .el-table__cell {
        .cell {
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }
    }

    .customer_item {
        span {
            width: 120px;
            color: #8fabbf;
        }
    }

    /deep/.el-form-item--small .el-form-item__label {
        color: #8fabbf !important;
    }
}
</style>
<style lang="less" scoped></style>